﻿$(function () {
    $('#tt').datagrid({
        loadMsg: "正在接收数据...",
        iconCls: 'icon-reload',
        url: '',
        idField: 'ParaItemID',
        autoRowHeight: false,
        fitColumns: true,
        scrollbarSize: 0,
        pageSize: 20,
        sortable: true,
        striped: true,      //隔行变色
        singleSelect: true, //是否单选
        pagination: true,   //分页控件
        rownumbers: true,
        onSortColumn: function (sort, order) {
            $("#hSort").val(sort);
            $("#rowName").val(order);
            loadLocal();
        }
    });
    $(window).resize(function () {
        $('#tt').datagrid('resize');
    });

    //初始化参数类型
    LoadType();
})

function LoadType() {
    $('#cboType').combobox({
        valueField: 'ParaID',
        textField: 'ParaName',
        panelHeight: "auto"
    });
    var MateialInfo = {
        RType: 1
    };
    DataService.PostJson("/api/SYS/GetParaInfo", MateialInfo, function (error, response) {
        if (error) {
            $('#cboType').combobox('loadData', response);
        }
    })
}

//查询
function loadLocal() {
    var TextName = $("#txtText").val();//编号
    var ParaID = $('#cboType').combobox('getValue');
    //排序字段
    var sort = $("#hSort").val();
    var order = $("#rowName").val();
    $("#tt").datagrid({
        url: '/api/SYS/QueryParaItem',
        method: 'Get',
        queryParams: {
            ItemName: TextName,
            ParaID: ParaID,
            RType: 1,
            sort: sort,
            order: order
        },
        loadFilter: function (data) {
            return JSON.parse(data);
        }
    });
    $(window).resize(function () {
        $('#tt').datagrid('reload');
    });
}

//新增弹窗
function IndexOpen() {
    var params = encodeURIComponent(JSON.stringify({
        RType: 1
    }));
    return OpenWindow("系统参数维护", "SYS/UI/SysParametersEdit.aspx?params=" + params, 420, 313,
        function () { loadLocal(); })
}

//删除
function Delet() {
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length < 1) {
        Alert("Please select！");
        return false;
    }
    Confirm("确定要删除吗？", function () {
        var MateialInfo = fnSelectList('tt', rows);
        var url = '/api/SYS/DeleteSysParaItem'
        DataService.PostJson(url, MateialInfo, function (error, response) {
            if (error) {
                Notice("success!");
            } else {
                Alert(response);
            }
            loadLocal();
        })
    });
}
//修改弹窗
function UpdateOpen(element) {
    var temp = decodeURIComponent(element.getAttribute('data-row'));
    var row = JSON.parse(temp);
    const params = encodeURIComponent(JSON.stringify(row));
    return OpenWindow("参数信息维护", "SYS/UI/SysParametersEdit.aspx?params=" + params, 420, 313,
        function () { $('#tt').datagrid('unselectAll'); $('#tt').datagrid('reload'); })
}

function ColumnOpt(value, row) {
    var temp = JSON.stringify(row);
    return "<a name='lock1' href='#' style='color:#a61c00' data-row='" + encodeURIComponent(temp) + "' onclick='UpdateOpen(this);'>[Update]</a>";
}